package com.xoa.controller.sysResource;


import com.xoa.dao.email.EmailBodyMapper;
import com.xoa.dao.email.EmailMapper;
import com.xoa.dao.users.UserExtMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.email.EmailBodyModel;
import com.xoa.model.users.Users;
import com.xoa.service.email.EmailService;
import com.xoa.service.sysResource.DiskSpaceDetailService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("/disk")
public class DiskSpaceDetailController {
        @Resource
        EmailMapper emailMapper;

        @Resource
        EmailBodyMapper emailBodyMapper;

        @Resource
        DiskSpaceDetailService diskSpaceDetailService;

        @Resource
        private EmailService emailService;

        @Resource
        private UsersMapper usersMapper;

        @Resource
        private UserExtMapper userExtMapper;


        @ResponseBody
        @RequestMapping("/diskSpace")  //  /disk/diskSpace
        public ToJson diskSpace(){
                return diskSpaceDetailService.GetDiskSpaceDetail();
        }

        //附件空间统计
        @ResponseBody
        @RequestMapping("showAttchmentSize")
        public ToJson showAttchmentSize(){return diskSpaceDetailService.showAttchmentSize();}

        //网络硬盘空间统计
        @ResponseBody
        @RequestMapping("showIntDiskSpace")
        public ToJson showIntDiskSpace(HttpServletRequest request){return diskSpaceDetailService.showIntDiskSpace(request);}


        @ResponseBody
        @RequestMapping("/email")// /disk/email
        public ToJson detail(){return diskSpaceDetailService.detail();}

        @ResponseBody
        @RequestMapping("/selectEmailAll")//     /disk/selectEmailAll
        public  ToJson selectEmailAll(EmailBodyModel emailBodyModel,Integer page,Integer pageSize,boolean useFlag){
                return diskSpaceDetailService.selectEmail(emailBodyModel,page,pageSize,useFlag);
        }

        /**
         * 删除接口
         */
        @ResponseBody
        @RequestMapping("/alldelete") // /disk/alldelete
        public ToJson Alldelete (String[] bodyId){
                return diskSpaceDetailService.Allemaildelete(bodyId);
        }
//
//        /**
//         * 创建作者:   左春晖
//         * 创建日期:   2017-4-20 上午10:35:16
//         * 方法介绍:   查询列表
//         * 参数说明:
//         * 参数说明:   @return json
//         * 参数说明:   @throws Exception
//         *
//         * @return String
//         */
//        @SuppressWarnings("all")
//        @RequestMapping(value = "/showEmail", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
//       // @RequestMapping("/showEmail")  //   /Disk/showEmail
//        @ResponseBody
//        public ToJson<EmailBodyModel> queryEmail(HttpServletRequest request, @RequestParam("flag") String flag,
//                                          @RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize,
//                                          @RequestParam("useFlag") boolean useFlag,
//                                          @RequestParam(value = "userID", required = false) String userFrom, @RequestParam(value = "startDate", required = false) String startDate,
//                                          @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "subject", required = false) String subject,
//                                          @RequestParam(value = "content", required = false) String content, @RequestParam(value = "attachmentName", required = false) String attachmentName,
//                                          @RequestParam(value = "readFlag", required = false) String readFlag, @RequestParam(value = "fromUserId", required = false) String fromUserId,
//                                          @RequestParam(value = "orderByName", required = false) String orderByName, @RequestParam(value = "orderWhere", required = false) String orderWhere,
//                                          @RequestParam(value = "searchCriteria", required = false) String searchCriteria, @RequestParam(value = "isWebmail", required = false) String isWebmail, String queryField) throws Exception {
//                String sqlType = "xoa" + (String) request.getSession().getAttribute(
//                        "loginDateSouse");
//                ContextHolder.setConsumerType(sqlType);
//                String userId = "";
//                if (StringUtils.checkNull(userFrom)) {
//                        userId = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users()).getUserId();
//                } else {
//                        userId = userFrom.trim();
//                }
//                Map<String, Object> maps = new HashMap<String, Object>();
//                maps.put("fromId", userId);
//                if (!StringUtils.checkNull(subject)) {
//                        maps.put("subject", "%" + subject + "%");
//                }
//                if (!StringUtils.checkNull(content)) {
//                        maps.put("content", "%" + content + "%");
//                }
//                if (!StringUtils.checkNull(attachmentName)) {
//                        maps.put("attachmentName", "%" + attachmentName + "%");
//                } else {
//                        maps.put("attachmentName", "");
//                }
//                if (!StringUtils.checkNull(startDate)) {
//                        maps.put("startTime", DateFormat.getTime(startDate));
//                }
//                if (!StringUtils.checkNull(endDate)) {
//                        maps.put("endTime", DateFormat.getTime(endDate));
//                }
//                if (!StringUtils.checkNull(searchCriteria)) {
//                        maps.put("searchCriteria", "%" + searchCriteria + "%");
//                }
//                maps.put("isWebmail", isWebmail);
//                maps.put("queryField", queryField);
//                maps.put("orderByName", returnMapValue().get(orderByName));
//                maps.put("orderWhere", returnMapValue().get(orderWhere));
//                ToJson<EmailBodyModel> tojson = new ToJson<EmailBodyModel>();
//                if (flag.trim().equals("inbox")) {
//                        maps.put("readFlag", readFlag);
//                        maps.put("fromUserId", fromUserId);
//                        tojson = emailService.selectInbox(request,maps, page, pageSize, useFlag, sqlType);
//                } else if (flag.trim().equals("drafts")) {
//                        tojson = emailService.listDrafts(maps, page, pageSize, useFlag, sqlType);
//                } else if (flag.trim().equals("outbox")) {
//                        tojson = emailService.listSendEmail(maps, page, pageSize, useFlag, sqlType);
//                } else if (flag.trim().equals("recycle")) {
//                        tojson = emailService.listWastePaperbasket(maps, page, pageSize,
//                                useFlag, sqlType);
//                } else if (flag.trim().equals("noRead")) {
//                        tojson = emailService.selectIsRead(maps, page, pageSize, useFlag, sqlType);
//                } else {
//                        tojson = emailService.selectEmail(maps, page, pageSize, useFlag, sqlType);
//                }
//                if (tojson.getObj().size() > 0) {
//                        tojson.setFlag(0);
//                        tojson.setMsg("ok");
//                } else {
//                        if(flag.trim().equals("drafts")){
//                                tojson.setFlag(0);
//                                tojson.setMsg("ok");
//                        }else{
//                                tojson.setFlag(1);
//                                tojson.setMsg("error");
//                        }
//                }
//                return tojson;
//        }
        /**
         * 创建作者:   张勇
         * 创建日期:   2017/7/5 15:02
         * 方法介绍:   倒序查询排序及条件
         * 参数说明:
         *
         * @return
         */
        @SuppressWarnings("all")
        public static Map<String, String> returnMapValue() {
                Map<String, String> maps = new HashMap<String, String>();
                maps.put("0", "ASC"); //升序
                maps.put("1", "DESC");//降序
                maps.put("2", "eb.SEND_TIME");//日期
                maps.put("3", "eb.FROM_ID");//发件人
                maps.put("4", "e.READ_FLAG");//是否已读
                maps.put("6", "eb.ATTACHMENT_ID");//附件
                return maps;
        }

        @RequestMapping("/system")  //  /disk/system
        public String show(HttpServletRequest request) {
                Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
                return "app/sys/sysManage";
        }
        @RequestMapping("/details")// /disk/details
        public String details(HttpServletRequest request) {
                Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
                return "app/sys/sysManageDetails";
        }
}